//app.js
import http from '/utils/http.js' 
App({
  onLaunch: function () {
    wx.getSystemInfo({
      success: e => {
        this.globalData.pixelRatio = e.globalData
        this.globalData.StatusBar = e.statusBarHeight;
        this.globalData.screenScale = e.screenWidth / 375
        let capsule = wx.getMenuButtonBoundingClientRect();
        if (capsule) {
          this.globalData.Custom = capsule;
          this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
        } else {
          this.globalData.CustomBar = e.statusBarHeight + 50;
        }
      }
    })
    http.request("/common/meta-data")
    .then(res=>{
      console.log('meta-data: ',res)
      this.globalData.serviceType = res.data.serviceType
      this.globalData.serviceMobile = res.data.serviceMobile
      this.globalData.mallUrl = res.data.mallUrl
      // 职业属性设置
      let jobProperty = res.data.jobProperty 
      let jobPropertyArray = []
      for (let i in jobProperty){
        jobPropertyArray.push({
          text: jobProperty[i],
          value: i
        })
      }
      this.globalData.jobProperty = jobPropertyArray

      // 饮水偏好设置
      let drinkPredilection = res.data.drinkPredilection
      let drinkPredilectionArray = []
      for (let i in drinkPredilection) {
        drinkPredilectionArray.push({
          text: drinkPredilection[i],
          value: i
        })
      }
      this.globalData.drinkPredilection = drinkPredilectionArray

      // 身体状况设置
      let health = res.data.health
      let healthArray = []
      for (let i in health) {
        healthArray.push({
          text: health[i],
          value: i
        })
      }
      this.globalData.health = healthArray
    })
    .catch(res=>{

    })
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        let params = {
          code: res.code
        }
        http.request("/auth/login",params)
        .then(res=>{
          this.globalData.session = res.data.session
          if(res.data.status == 1) { // 1 注册过 2 未注册
            wx.setStorageSync('token', res.data.token)
            this.globalData.needLogin = false
          }else{
            this.globalData.needLogin = true
          }
        })
        .catch(res=>{

        })
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    deviceCode: '',//微信扫码
    needLogin: false,
    lastPath: "", // 登录之后需要返回的地址
    userInfo: null,
    jobProperty: null, // 职业
    drinkPredilection: null, // 喝茶偏好
    health: null, // 健康状况
    warnTime: "", // 饮水方案设定/提醒时间设定
    serviceType: null, // 服务类型
    serviceMobile: "", // 400电话
    mallUrl: "", // 商城地址
    shareTitle: "天地精华",
    session: {}, // session
    isfirstMember: false, // 判断是否是户主
    pixelRatio: 1, // 分辨率
  }
})